﻿<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
    <title>综合治税GISAPP-后台管理中心</title>
    <link rel="stylesheet" href="Content/src/css/layui.css" media="all">
    
</head>
<body>
<div class="layui-tab layui-tab-brief main-tab-container ">
    <div class="layui-tab-content">
        <button class="layui-btn layui-btn-normal layui-btn-small demo" data-type="add"><i class="layui-icon">&#xe654;</i> 新 增 </button>
        <button class="layui-btn layui-btn-warm layui-btn-small demo" data-type="edit"><i class="layui-icon">&#xe642;</i> 编 辑 </button>
        <button class="layui-btn layui-btn-danger layui-btn-small" lay-submit lay-filter="delete"><i class="layui-icon">&#xe640;</i> 删 除 </button>
    </div>
    <div class="layui-form layui-tab-content">
      <table class="layui-table">
        <colgroup>
          <col width="80">
        </colgroup>
        <thead>
          <tr>
            <th><input name="checkAll" lay-skin="primary" type="checkbox" lay-filter="checkAll" ></th>
            <th>序号</th>
            <th>标题</th>
            <th>发布时间</th>
          </tr> 
        </thead>
        <tbody id="showNewList">

        </tbody>
      </table>
    </div>
</div>

<!-- Mode -->
<div id="add_edit" style="display: none;padding: 20px;">
    <form class="layui-form">
        <div class=" layui-form-item">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-block">
                <input type="text" id="Info_Title" lay-verify="required" autocomplete="off" class="layui-input">
                <input type="text" id="Info_Id" hidden>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">内容</label>
            <div class="layui-input-block">
                <textarea class="layui-textarea" name="content" id="Info_Mation" ></textarea>
            </div>
        </div>

        <div class="layui-form-item" style="text-align: right;">
            <div class="layui-btn layui-btn-normal" lay-submit lay-filter="btnSubmit">提 交</div>
        </div>
    </form>
</div>

<script type="text/javascript" src="Content/src/layui.js"></script>
<script src="Content/js/zor_base.js"></script>
<script type="text/javascript">
    layui.use(['element', 'layer', 'form', 'layedit'], function () {
        var element = layui.element()
        , jq = layui.jquery
        , form = layui.form()
        , layedit = layui.layedit
        , laypage = layui.laypage;

        //富文本编辑器
        var editIndex = layedit.build('Info_Mation');
       

        //加载，读取新闻列表
        jq(document).ready(function () {
            if (localStorage.getItem("username") == "" || localStorage.getItem("username") == null) {
                window.location.href = "login.html";
            } else {
                loading = layer.load();
                jq.ajax({
                    type: 'post',
                    url: '/api/Notice/NoticeList',
                    success: function (data) {
                        var html = '';
                        var accordionMenu = data;
                        jq.each(accordionMenu, function (i) {
                            html += "<tr>";
                            html += "<td><input name='' lay-skin='primary' type='checkbox' lay-filter='checkOne' role-id='" + accordionMenu[i].Info_Id + "' role-title='" + accordionMenu[i].Info_Title + "' role-mation='" + accordionMenu[i].Info_Mation + "'></td>";
                            html += "<td>" + (i + 1) + "</td>";
                            html += "<td>" + accordionMenu[i].Info_Title + "</td>";
                            html += "<td>" + accordionMenu[i].Info_Date + "</td>";
                            html += "</tr>";
                        });

                        jq('#showNewList').empty().append(html);
                        form.render('checkbox');
                        layer.close(loading);
                    }
                })
            }
        });


        //监听提交- 删除
        form.on('submit(delete)', function (data) {
            //判断是否有选项
            var ary = check(jq);
            if (ary.length == 0) {
                layer.msg('请选择要删除的数据！');
                return false;
            }
            layer.confirm("确认删除吗", { icon: 2, btn: ['确定', '取消'] }, function (index, layero) {
                jq.ajax({
                    type: 'post',
                    url: '/api/Notice/DelNotice',
                    contentType: "application/json",
                    data: JSON.stringify({ 'infomation': ary }),
                    dataType: "json",
                    success: function (data) {
                        if (data.AnswerType == 1) {
                            layer.close(loading);
                        }
                        layer.msg(data.AnswerMeg, function () {
                            location.reload();
                            return false;
                        });
                        Replace();
                    }
                });
            });
        });


        //全选
        form.on('checkbox(checkAll)', function (data) {
            if (data.elem.checked) {
                jq("input[type='checkbox']").prop('checked', true);
            } else {
                jq("input[type='checkbox']").prop('checked', false);
            }
            form.render('checkbox');
        });


        //单选
        form.on('checkbox(checkOne)', function (data) {
            var is_check = true;
            if (data.elem.checked) {
                jq("input[lay-filter='checkOne']").each(function () {
                    if (!jq(this).prop('checked')) { is_check = false; }
                });
                if (is_check) {
                    jq("input[lay-filter='checkAll']").prop('checked', true);
                }
            } else {
                jq("input[lay-filter='checkAll']").prop('checked', false);
            }
            form.render('checkbox');
        });


        //显示弹出框 - 增加、修改
        jq('.demo').on('click', function () {

            var type = jq(this).data('type');
            if (type == 'edit') {
                var ary = check(jq);
                if (ary.length == 0 || ary.length > 1) {
                    layer.msg('必须选择一行数据！');
                    return;
                }
            }

            layer.open({
                title: type,
                type: 1,
                skin: 'layui-layer-rim',
                area: ['700px', '530px'],
                content: jq('#add_edit'),
                success: function () {
                    if (type == 'edit') {
                        jq('#Info_Title').val(ary[0].Info_Title);
                        jq('#Info_Id').val(ary[0].Info_Id);
                        jq('#Info_Mation').val(ary[0].Info_Mation);
                        editIndex = layedit.build('Info_Mation');
                    }
                    else {
                        jq('#Info_Title').val('');
                        jq('#Info_Mation').val('');
                        jq('#Info_Id').val('');
                    }

                }
            });

        });


        //提交新增、编辑
        form.on('submit(btnSubmit)', function (data) {
            //获取富文本的值
            var Mation = layedit.getContent(editIndex);

            var Notice = {
                Info_Id: jq('#Info_Id').val(),
                Info_Title: jq('#Info_Title').val(),
                Info_Mation: Mation
            };

            var apiurl = "";
            if (Notice.Info_Id != '')
                apiurl = '/api/Notice/EditNotice';
            else
                apiurl = '/api/Notice/InsertNotice';

            loading = layer.load();

            jq.ajax({
                type: 'POST',
                url: apiurl,
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(Notice),
                success: function (data) {
                    if (data.AnswerType == 1) {
                        layer.close(loading);
                    }
                    layer.msg(data.AnswerMeg, function () {
                        Replace();
                    });
                }
            });
            
        });


        //返回 选择行的值
        function check() {
            var ary = new Array();
            jq("input[lay-filter='checkOne']").each(function () {
                if (jq(this).prop('checked')) {
                    var ckindex = jq(this).attr('role-id');
                    var cktname = jq(this).attr('role-title');
                    var ckreal = jq(this).attr('role-mation');
                    ary.push({ Info_Id: ckindex, Info_Title: cktname, Info_Mation: ckreal });
                }
            });
            return ary;
        }



    })
</script>

</body>
</html>